package com.nanyang.tms.service.supplier.impl;

import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.supplier.SupplierAddRequest;
import com.nanyang.tms.api.dto.request.supplier.SupplierQueryRequest;
import com.nanyang.tms.api.dto.request.supplier.SupplierUpdateRequest;
import com.nanyang.tms.api.dto.request.supplier.SupplierUpdateStateRequest;
import com.nanyang.tms.api.dto.response.supplier.SupplierPageKeyResponse;
import com.nanyang.tms.api.dto.response.supplier.SupplierPageResponse;
import com.nanyang.tms.api.dto.response.supplier.SupplierResponse;
import com.nanyang.tms.api.service.supplier.SupplierRpcService;
import com.nanyang.tms.dao.supplier.SupplierDao;
import com.nanyang.tms.domain.supplier.SupplierDO;
import com.nanyang.tms.domain.supplier.SupplierQuery;
import com.nanyang.tms.domain.supplier.SupplierVO;
import com.nanyang.tms.manager.supplier.SupplierManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class SupplierRpcServiceImpl implements SupplierRpcService {
    @Autowired
    private SupplierManager supplierManager;
    @Autowired
    private SupplierDao supplierDao;
    @Override
    public ApiResult<PageBean<SupplierPageResponse>> listSupplier(SupplierQueryRequest param) {
        ApiResult<PageBean<SupplierPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            SupplierQuery query = new SupplierQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<SupplierVO> supplierList = supplierManager.listSupplier(query);
            PageBean<SupplierPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(supplierList.getTotalItem());
            BeanUtils.copyListProperties(supplierList.getData(), resultList.getData(), SupplierPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<PageBean<SupplierPageKeyResponse>> listSupplierByKey(SupplierQueryRequest param) {
        ApiResult<PageBean<SupplierPageKeyResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            param.setState(0);//生效
            SupplierQuery query = new SupplierQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<SupplierVO> supplierList = supplierManager.listSupplier(query);
            PageBean<SupplierPageKeyResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(supplierList.getTotalItem());
            BeanUtils.copyListProperties(supplierList.getData(), resultList.getData(), SupplierPageKeyResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @Override
    public ApiResult<SupplierResponse> selectByCode(String code) {
        ApiResult<SupplierResponse> result = new ApiResult<>();
        try {
            SupplierResponse  supplierResult = new SupplierResponse();
            SupplierVO supplierVo = supplierDao.selectByCode(code);
            if(Objects.isNull(supplierVo)){
                throw new ServiceException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            BeanUtils.copyProperties(supplierVo,  supplierResult);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData( supplierResult);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> saveSupplier(SupplierAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            SupplierDO supplierDo = new  SupplierDO();
            BeanUtils.copyProperties(param,supplierDo);
            supplierDo.setGmtCreatePin(param.getPin());
            supplierManager.saveSupplier(supplierDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> updateSupplier(SupplierUpdateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            SupplierDO supplierDo = new  SupplierDO();
            BeanUtils.copyProperties(param,supplierDo);
            supplierDo.setGmtModifiedPin(param.getPin());
            supplierManager.updateSupplier(supplierDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    @SystemLog(module =SystemLog.Module.SUPPLIER,template="supplierDel")
    public ApiResult<Boolean> delSupplier(String supplierCode,String pin) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            supplierDao.removeState(supplierCode);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    @SystemLog(module =SystemLog.Module.SUPPLIER,template="supplierDisable")
    public ApiResult<Boolean> disableState(SupplierUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            SupplierDO supplierDo = new  SupplierDO();
            BeanUtils.copyProperties(param,supplierDo);
            supplierDo.setGmtModifiedPin(param.getPin());
            supplierDao.disableState(supplierDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    @SystemLog(module =SystemLog.Module.SUPPLIER,template="supplierEnable")
    public ApiResult<Boolean> enableState(SupplierUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            SupplierDO supplierDo = new  SupplierDO();
            BeanUtils.copyProperties(param,supplierDo);
            supplierDo.setGmtModifiedPin(param.getPin());
            supplierDao.enableState(supplierDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
